﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class shopcarTest_ShopCar : System.Web.UI.Page
{
    string cookie = "ShopCarTest";
    protected void Page_Load(object sender, EventArgs e)
    {
        ShopCar<TestP1> c1 = ShopCar<TestP1>.LoadFromCookie(cookie + "1");
        this.GridView1.DataSource = c1.AllProducts;
        this.GridView1.DataBind();

        ShopCar<TestP2> c2 = ShopCar<TestP2>.LoadFromCookie(cookie + "2");
        this.GridView2.DataSource = c2.AllProducts;
        this.GridView2.DataBind();

        Response.Write(c1.ToJSON()+"<br>");
        Response.Write(c2.ToJSON());

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ShopCar<TestP1> c1 = ShopCar<TestP1>.LoadFromCookie(cookie + "1");
        ShopCar<TestP2> c2 = ShopCar<TestP2>.LoadFromCookie(cookie + "2");

        OrderList ol = new OrderList();
        //可在此处设置订单的一些信息
        //ol.Discount = 1;
        //ol.Address = "";
        //ol.PostCode = "";

        c1.OnEachItemTransToOrderDetail += new ShopCar<TestP1>.ShopCarItemTransToOrderDetail<TestP1>(c1_OnEachItemTransToOrderDetail);
        c2.OnEachItemTransToOrderDetail += new ShopCar<TestP2>.ShopCarItemTransToOrderDetail<TestP2>(c2_OnEachItemTransToOrderDetail);

        Response.Write("<hr>把购物车内容的产品放入订单");
        ol.ShopCarAddToOrderList<TestP1>(c1);
        ol.ShopCarAddToOrderList<TestP2>(c2);

        ol.OnSubmiting += new OrderList.OrderListSubmitingEventHandler(ol_OnSubmiting);
        ol.OnEachOrderDetailSubmiting += new OrderDetail.OrderDetailSubmitingEventHandler(ol_OnEachOrderDetailSubmiting);
        ol.OnSubmited += new OrderList.OrderListSubmitedEventHandler(ol_OnSubmited);

        ol.OnEachOrderDetailSubmited += new OrderDetail.OrderDetailSubmitedEventHandler(ol_OnEachOrderDetailSubmited);

        Response.Write("<hr><br>开始提交");
        ol.SubmitToDB();
        Response.Write("<br>提交结束");
    }

    void ol_OnEachOrderDetailSubmited(OrderDetail sender, string OrderDetailPkValueInDB)
    {
        //每个订单明细(OrderDetail)提交完会执行此部分
        //OrderDetailPkValueInDB是订单明细在库中的ID
    }

    void ol_OnSubmited(OrderList sender, string OrderListPkValueInDB)
    {
        Response.Write("<br>全部订单提交完毕，清空购物车");
        ShopCar<TestP1> c1 = ShopCar<TestP1>.LoadFromCookie(cookie + "1");
        ShopCar<TestP2> c2 = ShopCar<TestP2>.LoadFromCookie(cookie + "2");
        //c1.Clear();
        //c2.Clear();
    }

    void ol_OnEachOrderDetailSubmiting(OrderDetail sender, OrderDetailSubmitEventArgs args)
    {
        Response.Write("<br>开始提交订单明细");
        //构造并执行sql

        //如果是通过EventArgs传递的，OrderID会自动赋值，如果不是需要手动赋值
        //sender.OrderID = this.OrderID;
        //也可在此对订单明细的一些信息做处理
        //sender.ProductURL = "";

        //可检测各种条件，如果不符合，可Canel提交
        //args.Canel = true;

        string sql = "insert into orderdetail(orderid,pid,pname,price) values (" + sender.OrderID + "," + sender.ProductID + "," + sender.ProductName + "," + sender.SinglePrice + ")";
        Response.Write(sql);

        //如果需要在提交完成后执行某些操作，可传递此订单明细在库中的ID
        //args.OrderDetailPKValueInDB = "1";

        Response.Write("<br>");
        Response.Write("<br>订单明细提交结束");
    }

    //可设置全局变量来获得订单列表提交到数据库里的ID
    int OrderID = 0;
    void ol_OnSubmiting(OrderList sender, OrderListSubmitEventArgs args)
    {
        //也可在此处设置一些订单的信息
        //sender.Discount = 0.9F;
        //sender.Address = "1";
        //sender.PostCode = "123456";

        //可检测各种条件，如果不符合，可Canel提交
        //args.Canel = true;

        Response.Write("<br>开始提交订单");
        //构造并执行sql
        string sql = "insert into orderlist (allproductprice) values (" + sender.AllProductPrice + ")";
        Response.Write("<br>");
        Response.Write(sql);
        Response.Write("<hr>");

        //获取并赋值，传回订单在库中的ID
        //string sql = "select max(id) from orderlist";
        args.OrderListPKValueInDB = "123";
        //也可用全局变量传
        this.OrderID = 234;
        Response.Write("<br>订单提交结束并返回订单主键为123");
    }

    void c2_OnEachItemTransToOrderDetail(TestP2 Product, OrderDetail OrderDetail)
    {
        Response.Write("<br>开始转换TestP2类型的产品");
        OrderDetail.ProductID = Product.ID;
        OrderDetail.ProductName = Product.Name;
        OrderDetail.SinglePrice = Product.JG;
        Response.Write("<br>TestP2类型产品转换结束");
    }

    void c1_OnEachItemTransToOrderDetail(TestP1 Product, OrderDetail OrderDetail)
    {
        Response.Write("<br>开始转换TestP1类型产品");
        OrderDetail.ProductID = Product.PK;
        OrderDetail.ProductName = Product.Name;
        OrderDetail.SinglePrice = Product.Price;
        Response.Write("<br>TestP1类型产品转换结束");
    }    
}
